package org.example.datascreen;

import org.example.datascreen.eneity.User;
import org.example.datascreen.repository.DeviceRespository;
import org.example.datascreen.repository.UserRepository;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Date;
import java.util.List;

@SpringBootTest

public class Usertest {

    @Autowired
    private UserRepository userRepository;
    @Autowired
    private DeviceRespository deviceRepository;

    @Test
    public void testUser() {
        User user = new User();
        user.setUsername("zxb");
        user.setPassword("159357");
        user.setCreatedTime(new Date());
        userRepository.save(user);
    }

    @Test
    public void testFindUser() {
        String username = "zxb";
        String password = "159357";
        User user = userRepository.findByUsernameAndPassword(username, password);
        System.out.println(user);
    }


    @Test
    public void testDevice() throws JSONException {
        List<String> serialNumbers = deviceRepository.findSerialNumbersByCategory("Sensor");
        System.out.println(serialNumbers);
        JSONObject jsonBody = new JSONObject();
        JSONArray ids = new JSONArray();
        for (String serialNumber : serialNumbers) {
            ids.put(serialNumber);
        }
        jsonBody.put("ids", ids);
        System.out.println(jsonBody.toString());
    }
}
